﻿using NPOI.SS.UserModel;
using Sunny.UI;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace CoreShop.App
{
    public partial class UIDishItem : UIUserControl
    {
        public UIDishItem()
        {
            InitializeComponent();
            Info = new DishItemInfo();
            Text = Info.Name;
        }


        private Image image;
        public Image Image
        {
            get { return image; }
            set
            {
                image = value;
                Width = value == null ? 133 : 202;
                Invalidate();
            }
        }

        public DishItemInfo Info { get; }
        public UIDishItem(DishItemInfo info)
        {
            InitializeComponent();
            if (info == null)
            {
                throw new NullReferenceException("info");
            }

            Info = info;
            Text = info.Name;
        }

        public string TextName
        {
            get => Text;
            set
            {
                Text = value;
                Invalidate();
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (Info == null) return;
            int Added = (image != null) ? 75 : 0;

            if (image != null)
            {
                e.Graphics.DrawImage(Image, new Rectangle(5, 6, 78, 78), new Rectangle(0, 0, Image.Width, image.Height), GraphicsUnit.Pixel);
            }

            if (Info.LabelExist.IsValid())
            {
                using (Font font = new Font("微软雅黑", 7.5f))
                {
                    e.Graphics.DrawString(Info.LabelExist, font, Color.IndianRed, Added + 10, 1);
                }
            }

            RectangleF descRect = new RectangleF();
            using (Font font = new Font("微软雅黑", 13f, System.Drawing.FontStyle.Bold))
            {
                descRect.Location = new Point(Added + 10, 4);
                //descRect.Size = new Size(115, ((int)e.Graphics.MeasureString(Text, font, 115, StringFormat.GenericTypographic).Height));
                descRect.Size = new Size(120, 70);
                e.Graphics.DrawString(Text, font, Brushes.DimGray, descRect);
            }

            using (Font font = new Font("微软雅黑", 9f))
            {
                if (Info.DiscountString.IsValid())
                {
                    SizeF sf = e.Graphics.MeasureString(Info.DiscountString, font);
                    e.Graphics.DrawString(Info.DiscountString, font, Color.Red, Added + 10, descRect.Top + descRect.Height + 2);
                    e.Graphics.DrawRoundRectangle(Color.FromArgb(255, 186, 33), new Rectangle(Added + 10, (int)(descRect.Top + descRect.Height + 2), (int)sf.Width, (int)sf.Height - 1), 5);
                }
            }

            SizeF textSize = new SizeF();
            using (Font font = new Font("微软雅黑", 12f))
            {
                e.Graphics.DrawString(Info.Sign + Info.Money, font, Color.Chocolate, Added + 8, 70);
                textSize = e.Graphics.MeasureString(Info.Sign + Info.Money, font);                
            }
            if (Info.UnitName.IsValid())
            {
                using (Font font = new Font("微软雅黑", 10f))
                {
                    e.Graphics.DrawString("/" + Info.UnitName, font, Color.Gray, Added + 5 + textSize.Width, 72);
                }
            }

            if (TipsText.IsValid())
            {
                using (Font font = new Font("微软雅黑", 12f))
                {
                    e.Graphics.SetHighQuality();

                    Size sf = TextRenderer.MeasureText(TipsText, font);
                    int sfMax = Math.Max(sf.Width, sf.Height);
                    int x = Width - 1 - sfMax;
                    int y = 1;
                    e.Graphics.FillEllipse(Color.FromArgb(195, 59, 61), x - 1, y, sfMax, sfMax);
                    e.Graphics.DrawString(TipsText, font, Color.White, new Rectangle(x, y, sfMax, sfMax), ContentAlignment.MiddleCenter);
                }
            }
        }

        public string TipsText { get; set; }
    }
}